package com.eqdd.yiqidian.ui.gongneng.renshi.kaoqin;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.rebuild.QingjiaSetListResultBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.SwipeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-11-18  下午2:26.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class QingjiaSetListActivity extends BaseActivity {

    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<QingjiaSetListResultBean> slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "请假设置");
        initTopRightText("添加", v -> {

        });

    }

    @Override
    public void initData() {
        SwipeUtil.openSwipe(this, dataBinding.recyclerView, (menuBridge, pos) -> {
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.DELETE_QINGJIA_SET)
                    .params("userGuid", AppConstent.user.getGuid())
                    .params("companyId", AppConstent.user.getCompanyId())
                    .params("id", slimAdapterEx.getDataItem(pos).getId())
                    .execute(new DialogCallBack<HttpResult>(this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                menuBridge.closeMenu();
                                slimAdapterEx.getData().remove(pos);
                                slimAdapterEx.notifyItemRemoved(pos);
                            }
                        }
                    });

        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_12, new SlimInjector<QingjiaSetListResultBean>() {
            @Override
            public void onInject(QingjiaSetListResultBean data, IViewInjector injector) {

                injector.text(R.id.tv_upper_left_content, "请假天数范围")
                        .text(R.id.tv_upper_right_content, data.getMinTime() + "~" + data.getMaxTime() + "天")
                        .text(R.id.tv_down_left_content, "审批等级")
                        .text(R.id.tv_down_left_content, data.getApprovalLevel() + "级审批");
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
    }

    @Override
    public void setView() {

        OkGo.<HttpResult<List<QingjiaSetListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.QINGJIA_SET_LIST)
                .params("userGuid", AppConstent.user.getGuid())
                .params("companyId", AppConstent.user.getCompanyId())
                .params("type", 1)
                .execute(new DialogCallBack<HttpResult<List<QingjiaSetListResultBean>>>(QingjiaSetListActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<List<QingjiaSetListResultBean>>> response) {
                        HttpResult<List<QingjiaSetListResultBean>> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {

                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                    }
                });
    }
}
